﻿function CommentVm(p) {
    var self = this;
    this.post = p;
    this.id = ko.observable();
    this.fileId = ko.observable();
    this.name = ko.observable('');
    this.comment = ko.observable('');
    this.url = ko.observable('');
    this.time = ko.observable();
    this.isAuthor = ko.observable(false);

    this.showPostLink = ko.observable(false);
    this.postLink = ko.computed(function () {
        return appPath + 'Post/' + this.fileId();
    }, this);

    this.className = ko.computed(function () {
        if (this.isAuthor())
            return 'comment-author';
        else
            return '';
    }, this);
    this.isApproved = ko.observable();

    this.errors = ko.observableArray();
    this.errorMessage = ko.computed({
        read: function () {
            var html = '';
            if (this.errors().length > 0)
                html += this.errors()[0];
            if (this.errors().length > 1) {
                html += "<ul>"
                for (var idx = 1; idx < this.errors().length; idx++)
                    html += "<li>" + this.errors()[idx] + "</li>";
                html += "</ul>";
            }
            return html;
        },
        write: function (value) {
            this.errors.removeAll();
            this.errors.push(value);
        },
        owner: this
    });

    this.loadJson = function (comment) {
        this.id(comment.ID);
        this.fileId(comment.FileID);
        this.name(comment.Name);
        this.comment(comment.Text);
        this.time(comment.FormattedTime);
        this.url(comment.Url);
        this.isAuthor(comment.IsAuthor);
        this.isApproved(comment.IsApproved);
    }

    this.addComment = function () {

        var postVm = p;
        // call the web api
        var path = appPath + 'Comment/Create';
        var self = this;
        var input = {
            FileID: self.fileId(),
            Name: self.name(),
            RawComment: self.comment(),
            Url: self.url(),
            IsAuthor: self.isAuthor()
        };
        
        $.post(path, input, function (data) {
            if (!data.Result) {
                self.errorMessage(data.Message);
                if (data.Errors) {
                    for (var idx = 0; idx < data.Errors.length; idx++)
                        self.errors.push(data.Errors[idx]);
                }
            }
            else {
                postVm.insertCommentMessage(data.Message);
                setTimeout(function () { postVm.insertCommentMessage(''); }, 3000);
                
                var comment = data.Data;
                var commentVm = new CommentVm(postVm);
                commentVm.loadJson(comment);
                if (commentVm.isApproved()) {
                    postVm.comments.unshift(commentVm);
                    postVm.commentCount(postVm.commentCount() + 1);
                }
                postVm.showForm(false);
            }
        });
    };

    this.closeComment = function () {
        var postVm = p;
        postVm.showForm(false);
    }
}